<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>员工管理页面</title>
    <link rel="stylesheet" href="../bootstrap/css/bootstrap.css">

    <style>
        body {
            margin:0;
            padding:0;
            font-family:sans-serif;
            background:#2C2255;
            background:url(../image/水漫漫.jpg);
            background-repeat: no-repeat;
            background-size:100%;
            font-size: 20px;
            font-family:STXingkai;
            color: #59ec0a;
        }
        a{
            text-decoration:none;
        }
        button{
            margin:10px;
        }
        th{
            width:300px;

        }
        td{
            width:200px;
            height:80px;
        }
    </style>
</head>
<body>
<div id="employee">
    <table id="employee-table" >
        <button @click="insert()" class="btn btn-warning" id="inser-employee" >添加新员工</button>
        姓名：<input id="name" placeholder="请输入要查询的姓名">
        职位：<input id="position"  placeholder="请输入要查询的职位">
        <button @click="selEmployee" class="btn btn-warning">查询员工</button>
        <button @click="delEmployee" id="dleEmployee" class="btn btn-danger">删除员工</button>
        <tr>
            <th ><input id="checkAll" type="checkbox" @click="allCheck"></th>
            <th>员工编号</th>
            <th>员工姓名</th>
            <th>员工性别</th>
            <th>身份证号</th>
            <th>手机号码</th>
            <th>家庭住址</th>
            <th>员工职业</th>
            <th>操作</th>
        </tr>
        <tr v-for="employee in list" @click="trCheck(event)">
            <td ><input class="check" type="checkbox" :value="employee.employeeId" @click="isCheckAll"></td>
            <td >{{employee.employeeId}}</td>
            <td >{{employee.employeeName}}</td>
            <td >{{employee.employeeSex==1?"男":"女"}}</td>
            <td >{{employee.employeeIdCard}}</td>
            <td >{{employee.employeePhoneNumer}}</td>
            <td >{{employee.employeeAddress}}</td>
            <td ><span v-for="job in employee.jobList" style="padding-left:3px ">{{job.jobTitle}}</span><br></td>
            <td>
                <button v-on:click="show(employee)" data-toggle="modal" data-target="#myModal" class="btn btn-danger modify">修改</button>
            </td>
        </tr>
    </table>
    <!--            分页区域-->
    <div class="pagination-div">
        <ul class="pagination" id="page-ul" >
            <li class="page-item">
                <a class="page-link" href="javascript:void(0);" @click="cutPage(1)">首页</a>
            </li>
            <li class="page-item" v-if="page.pageNow!=1">
                <a class="page-link" href="javascript:void(0);"
                   @click="cutPage(page.pageNow-1)">{{page.pageNow-1}}</a></li>
            <li class="page-item active">
                <a class="page-link" href="javascript:void(0);">{{page.pageNow}}</a>
            </li>
            <li class="page-item" v-if="page.pageNow!=page.pageTotal">
                <a class="page-link" href="javascript:void(0);"
                   @click="cutPage(page.pageNow+1)">{{page.pageNow+1}}</a>
            </li>
            <li class="page-item">
                <a class="page-link" href="javascript:void(0);"
                   @click="cutPage(page.pageTotal)">尾页</a>
            </li>
            <li class="page-item">
                <a class="page-link" href="javascript:void(0);">共有{{page.count}}条数据</a>
            </li>
            <li class="page-item">
                <a class="page-link" href="javascript:void(0);">共有{{page.pageTotal}}页</a>
            </li>
        </ul>
    </div>

    <!-- 模态框（Modal） -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog"  >
            <div class="modal-content" id="myModal-smile" style=" height: 690px;margin: 0">
                <div class="modal-body" >
                    <form id="emp-form">
                        <input type="hidden" class="form-control" id="employeeId" name="employeeId" v-model="employee.employeeId">
                        <div class="form-group">
                            <label for="employeeName">员工名称:</label>
                            <input type="text" class="form-control" id="employeeName" name="employeeName" v-model="employee.employeeName" >
                        </div>

                        <div class="form-group">
                            <label for="employeeIdCard">身份证信息：</label>
                            <input type="number" class="form-control"  id="employeeIdCard" name="employeeIdCard" v-model="employee.employeeIdCard">
                        </div>

                        <div class="form-group">
                            <label for="employeePhoneNumer">手机号码：</label>
                            <input type="number" class="form-control" id="employeePhoneNumer" name="employeePhoneNumer" v-model="employee.employeePhoneNumer">
                        </div>

                        <div class="form-group">
                            员工性别：
                            <label class="radio-inline">
                                <input type="radio" name="employeeSex" id="optionsRadios3" value="1" checked v-model="employee.employeeSex"> 男
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="employeeSex" id="optionsRadios4"  value="2" v-model="employee.employeeSex"> 女
                            </label>
                        </div>


                        <div class="form-group">
                            <label class="col-sm-4 control-label">职业选择：</label>
                            <div class="col-sm-3">
<!--                                v-model="employee.jobList[index]"-->
                                <select v-model="employee.jobList" class="selectpicker" multiple data-live-search="true" data-selected-text-format="count > 3" name="jobList" id="jobList">
                                    <option v-for="job in jobList" v-text="job.jobTitle" :value="job.jobId"></option>

                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="employeeAddress">家庭住址a：</label>
                            <input type="text" class="form-control" id="employeeAddress" name="employeeAddress" v-model="employee.employeeAddress">
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" id="refresh" class="btn btn-default" @click="close()" data-dismiss="modal">提交并关闭</button>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript" src="../css/jquery-3.4.1.js"></script>
<script src="../bootstrap/js/bootstrap.js" ></script>
<script src="../vue/vue.js" charset="utf-8"></script>
<script type="text/javascript" src="../css/my.js"></script>


    <script>
        let employeeTabVue
        //给职业下拉框填充数据
        function selJob () {
            let map = [];
            $.ajax({
                type:"post",
                url:"../JobController/selJob",
                data:"",
                async:false,
                dataType:"json",
                success:function (json) {
                    map = json;
                }
            })
            return map;
        }
        let jobs = selJob();

        // 查询图书 需要接受一个当前页参数
        function selemployee(pageNow,name,position) {
            let map = [];
            $.ajax({
                type: "post",
                url: "../EmployeeController/selEmployee",
                dataType: "json",
                data:{pageNow:pageNow, pageCount:5,employeeName:name,jobTitle:position},
                async: false,
                success: function (data) {
                    map = data;
                }
            });
            return map;
        }
        /*创建一个map装查询到的第一页的数据*/
        let map = selemployee(1,null,null);
        //加载分页数据
        employeeTabVue = new Vue({
            el: '#employee',
            data: {
                list: map.employeeList,// 展示表格
                jobList: jobs.list,//给职业下拉框填充数据
                page: map, // 用于分页
                employee: { // 修改窗口的对象数据
                    employeeId: "",
                    employeeName: "",
                    employeeSex: "",
                    employeeIdCard: "",
                    employeePhoneNumer: "",
                    employeeAddress: "",
                    jobList: []
                }
            },
            methods: {
                //切换页方法 参数为当前按钮所代表的对应页
                cutPage: function (pageNow) {
                    let name = $("#name").val();
                    let position = $("#position").val();
                    //查询要切换页的数据。把结果从新给表格赋值
                    employeeTabVue.list = selemployee(pageNow,name,position).employeeList;
                    //把分页的vue重新赋值。
                    employeeTabVue.page.pageNow = pageNow;
                },
                show:function (employee) {
                    //创建jobListId数组用来装入jobId
                    let jobIds = new Array();
                    let jobList = employee.jobList
                    //循环对象中的job数组
                    for (let i = 0; i < jobList.length; i++) {
                        //将job中的id装入jobIds中
                        jobIds[i] = jobList[i].jobId;
                    }
                    //给对象中的jobList重新赋值
                    employee.jobList=jobIds;
                    this.employee = employee;
                    console.log(employee);
                },
                 //模态框关闭提交修改
                close:function () {
                    let emp=$("#emp-form").serialize();
                    let value2=$('#jobList').val();
                    //let value=value2.join(',');
                    console.log(value2);

                    $.ajax({
                        type:"post",
                        url:"../EmployeeController/updEmp",
                        data:emp+"&value2="+value2,
                        dataType:"json",
                        success:function (json) {
                            alert(json.msg);
                            if (json.msg=="修改成功！"){
                                //window.location.reload();
                            }
                        }
                    })
                    //window.location.reload();
                },
                //条件查询重新调用查询方法
                selEmployee:function(){
                    //获取两个input框的数据
                    let name = $("#name").val();
                    let position = $("#position").val();
                    employeeTabVue.list = selemployee(1,name,position).employeeList;
                    employeeTabVue.page = selemployee(1,name,position);
                },
                allCheck:function () {
                    eventHub.$emit("allCheck",null);
                },
                isCheckAll:function () {
                    eventHub.$emit("isCheckAll",null);
                },
                trCheck:function (event) {
                    eventHub.$emit("trCheck",event);
                },
                insert:function () {
                    window.location.href="insertEmp.html";
                },
                delEmployee:function () {
                    let empIds = '';
                    //获取被选中的复选框的id值 each()遍历
                    $(".check").each(function () {
                        //对所有符合该选择器的元素进行操作
                        //判断该复选框是否选中
                        if(this.checked){

                            empIds+=this.value + ",";
                            if(empIds=="") return;
                            $.ajax({
                                type: "post",
                                url: "../EmployeeController/delEmployee",
                                data: {empIds: empIds},
                                dataType: "json",
                                success: function (data) {
                                    alert(data.msg);
                                    window.location.reload();
                                }
                            });
                        }
                        console.log(empIds);
                    });
                }
            }
        });

    </script>
</body>
</html>